﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Core.Config
{
    public static class AccessDatSerializer
    {
        public static readonly object LockObject = new object();
        /// <summary>
        /// 将对象序列化为指定Dat文件
        /// </summary>
        /// <param name="filePath">路径</param>
        /// <param name="sourceObj">对象</param>
        /// <returns>是否成功</returns>
        public static bool ObjectToDat(string filePath, object sourceObj)
        {
            lock (LockObject)
            {
                if (!string.IsNullOrWhiteSpace(filePath) && sourceObj != null)
                {
                    string folderPath = filePath.Substring(0, filePath.LastIndexOf(@"\"));
                    if (Directory.Exists(folderPath) == false)
                    {
                        Directory.CreateDirectory(folderPath);
                    }
                    try
                    {
                        using (FileStream fs = new FileStream(filePath, FileMode.Create))
                        {
                            BinaryFormatter formatter = new BinaryFormatter();
                            formatter.Serialize(fs, sourceObj);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message, ex);
                    }
                }
                else
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 将指定Dat反序列化为对象
        /// </summary>
        /// <param name="filePath">Dat路径</param>
        /// <returns>object</returns>
        public static object DatToObject(string filePath)
        {
            object result = null;
            lock (LockObject)
            {
                if (File.Exists(filePath))
                {
                    try
                    {
                        using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                        {
                            //fs.Seek(0, SeekOrigin.Begin);
                            BinaryFormatter formatter = new BinaryFormatter();
                            result = formatter.Deserialize(fs);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message,ex);
                    }
                }
                else
                {
                    return null;
                }
                return result;
            }
        }

        /// <summary>
        /// 将指定XML反序列化为对象
        /// </summary>
        /// <typeparam name="T">对象</typeparam>
        /// <param name="filePath">路径</param>
        /// <returns>对象</returns>
        public static T DatToObject<T>(string filePath)
        {
            return (T)DatToObject(filePath);
        }
    }
}
